//
//  VideoListViewController.m
//  BosidengIphone
//
//  Created by  on 11-12-31.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

//#import <MediaPlayer/MediaPlayer.h>
#import "VideoListViewController.h"
#import "SyncImageView.h"
#import "VideoData.h"
#import "VideoPlayViewController.h"

@implementation VideoListViewController
@synthesize scrollView;
@synthesize videos;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.videos = [VideoData getAllItems];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
    [videos release];
    [scrollView release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)initVideoView:(VideoData *)data andFrame:(CGRect)frame andIndex:(NSInteger)index {
    UIView *rootView = [[UIView alloc] initWithFrame:frame];
    [scrollView addSubview:rootView];
    [rootView release];
    
    int textLabelHeight = 23;
    NSString *imageUrl = data.image;
    SyncImageView *imageView = [[SyncImageView alloc] initWithUrl:imageUrl andRequestQueue:requestQueue];
    imageView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height - textLabelHeight);
    [rootView addSubview:imageView];
    [imageView release];
    
    UILabel *labText = [[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height - textLabelHeight, frame.size.width, textLabelHeight)];
    labText.text = data.title;
    labText.font=[UIFont systemFontOfSize:9];
	labText.textAlignment=UITextAlignmentCenter;
    [rootView addSubview:labText];
    [labText release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    button.tag = 100 + index;
    [rootView addSubview:button];
    [button addTarget:self action:@selector(imageClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)jumpToVideo:(NSString *)str {
    NSURL *url = [NSURL URLWithString:str];
    //MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    VideoPlayViewController *player = [[VideoPlayViewController alloc] initWithContentURL:url];
    [mainViewController pushViewController:player];
    [player release];
}

- (void)imageClick:(id)sender {
	UIButton *imageButton = (UIButton *)sender;
	int index = imageButton.tag - 100;
    VideoData *data = [videos objectAtIndex:index];
    [self jumpToVideo:data.videoUrl];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    int padding = 10;
    int imageWidth = 145, imageHeight = 132;
    int cols = 2;
    
    int x = 0, y = 0;
    int rowIndex = 0, colIndex = 0;
    CGRect frame;
    VideoData *data;
    for (int i = 0; i < [videos count]; i++) {
        rowIndex = i % cols;
        colIndex = ceilf(i / cols);
        x = padding + rowIndex * (imageWidth + padding);
        y = padding + colIndex * (imageHeight);
        frame = CGRectMake(x, y, imageWidth, imageHeight);
        data = [videos objectAtIndex:i];
        [self initVideoView:data andFrame:frame andIndex:i];
    }
    scrollView.contentSize=CGSizeMake(320, y+imageHeight);
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
